package com.liukai.bishe.ui.activity;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.bigkoo.pickerview.builder.TimePickerBuilder;
import com.bigkoo.pickerview.view.TimePickerView;
import com.liukai.bishe.R;
import com.liukai.bishe.model.doman.transaction.TransactionUseTime;
import com.liukai.bishe.ui.adapter.RecordRecyclerViewAdapter;
import com.liukai.bishe.utils.Constants;
import com.liukai.bishe.utils.DateUtils;
import com.liukai.bishe.utils.ToastUtil;
import com.robinhood.ticker.TickerUtils;
import com.robinhood.ticker.TickerView;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

public class RecordActivity extends AppCompatActivity {

    private List<TransactionUseTime> mTransactionUseTimeRecordList;
    private Unbinder mBind;

    private boolean isNewChoose = false;
    private long newTimeMills = 0;

    @BindView(R.id.recyclerView)
    public RecyclerView mRecyclerView;

    @BindView(R.id.btnBack)
    public ImageView btnBack;

    @BindView(R.id.tvTotalCount)
    public TickerView tvTotalCount;

    @BindView(R.id.tvTotalTime)
    public TickerView tvTotalTime;

    @BindView(R.id.btnChooseDate)
    public ImageView btnChooseDate;

    @BindView(R.id.btnDate)
    public TextView btnDate;

    private Context mContext = RecordActivity.this;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_record);

        mBind = ButterKnife.bind(this);
        initData();


    }

    private void initListener() {

        btnBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });

        btnChooseDate.setOnClickListener(view -> {
            TimePickerView pvTime = new TimePickerBuilder(mContext, (date, v) -> {
//                    SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
                Toast.makeText(mContext, date.getTime()+"", Toast.LENGTH_SHORT).show();
                newTimeMills = date.getTime();
                isNewChoose = true;
                initData();
//                    Intent intent = new Intent(mContext,RecordActivity.class);
//                    intent.putExtra("timeMills",String.valueOf(timeMills));
//                    mContext.startActivity(intent);
            }).setType(new boolean[]{true, true, true, false, false, false})// 默认全部显示
                    .setCancelText("Cancel")//取消按钮文字
                    .setSubmitText("Sure")//确认按钮文字
                    .setTitleSize(20)//标题文字大小
                    .setTitleText("Title")//标题文字
                    .setOutSideCancelable(false)//点击屏幕，点在控件外部范围时，是否取消显示
                    .isCyclic(true)//是否循环滚动
                    .setTitleColor(mContext.getColor(R.color.white))//标题文字颜色
                    .setSubmitColor(mContext.getColor(R.color.white))//确定按钮文字颜色
                    .setCancelColor(mContext.getColor(R.color.white))//取消按钮文字颜色
                    .setTitleBgColor(mContext.getColor(R.color.colorPrimary))//标题背景颜色 Night mode
                    .setBgColor(mContext.getColor(R.color.white))//滚轮背景颜色 Night mode
                    .setLabel("年","月","日","时","分","秒")//默认设置为年月日时分秒
                    .isCenterLabel(false) //是否只显示中间选中项的label文字，false则每项item全部都带有label。
                    .isDialog(true)//是否显示为对话框样式
                    .setTextColorCenter(mContext.getColor(R.color.colorPrimary))
                    .setTextColorOut(mContext.getColor(R.color.colorNoSexGrey))
                    .build();
            pvTime.show();
        });

    }

    private void initAdapter() {
        RecordRecyclerViewAdapter adapter = new RecordRecyclerViewAdapter(mContext,mTransactionUseTimeRecordList);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
        mRecyclerView.setLayoutManager(linearLayoutManager);
        mRecyclerView.setAdapter(adapter);
    }

    private void initData(){
        String timeMills = "";
        if (isNewChoose){
            timeMills = String.valueOf(newTimeMills);
        }else{
            timeMills = getIntent().getStringExtra("timeMills");
        }
        isNewChoose = false;
        String startDate = DateUtils.timeMillisToTime("yyyy-MM-dd",timeMills);
        btnDate.setText(DateUtils.timeMillisToTime("yyyy年MM月dd日",timeMills));
        mTransactionUseTimeRecordList = new ArrayList<>();
        long totalTimeMills = 0;
        List<TransactionUseTime> transactionUseTimeList = Constants.getTransactionUseTimeList();
        for (TransactionUseTime transactionUseTime : transactionUseTimeList) {
            String startDate0 = transactionUseTime.getStartdate();
            if (startDate.equals(transactionUseTime.getStartdate())) {
                mTransactionUseTimeRecordList.add(transactionUseTime);
                totalTimeMills += Long.valueOf(transactionUseTime.getThistotaltime());
            }
        }
        tvTotalCount.setCharacterLists(TickerUtils.provideNumberList());
        tvTotalTime.setCharacterLists(TickerUtils.provideNumberList());
        tvTotalCount.setText(mTransactionUseTimeRecordList.size()+"");
        tvTotalTime.setText(DateUtils.longTimeToMinuties(totalTimeMills));
//        for (TransactionUseTime transactionUseTime : mTransactionUseTimeRecordList) {
//
//        }

        initAdapter();
        initListener();

    }

}
